<template>
  <div class="base">
    <div class="topbar">
      <div class="logobox">
        <h1>智慧信访平台</h1>
      </div>
      <div class="navbarbox">
        <ul class="nav">
          <li><a href="#home">首页</a></li>
          <li><a href="#government">政务公开</a></li>
          <li><a href="#affairs">事务办理</a></li>
          <li><a href="#interaction">交流互动</a></li>
          <li><a @click="pageToVideoTalk">联系我们</a></li>
        </ul>
      </div>
      <div class="loginbox">
        <ul class="login-nav" v-if="isLogin">
          <li><a href="#no">无障碍</a></li>
          <li><a @click="pageToLogin">登录</a></li>
          <li><a @click="pageToRegister">注册</a></li>
          <li><a href="https://www.gov.cn/">中国政府网</a></li>
        </ul>
        <ul class="login-nav" v-else>
          <li><a href="#no">无障碍</a></li>
          <li><a @click="pageToInfo">个人中心</a></li>
          <li><a @click="logout">退出登录</a></li>
          <li><a href="https://www.gov.cn/">中国政府网</a></li>
        </ul>
      </div>
    </div>
    <div class="bodybox">
      <div class="topbanner">
        <img src="../assets/icon/serviceicon.png">
        <h1>将为人民服务的精神落实到这片大地上</h1>
      </div>
      <div class="leftbox">
        <div class="searchbox">
          <div class="search-box">
            <input type="text" class="search-input" placeholder="请输入关键字">
            <button type="button" class="search-button">搜索</button>
          </div>
        </div>
        <div class="mainnewsbox">
          <el-carousel indicator-position="outside" height="420px">
            <el-carousel-item>
              <img src="@/assets/img/mainnewsimg2.png">
            </el-carousel-item>
            <el-carousel-item>
              <img src="@/assets/img/mainnewsimg.png">
            </el-carousel-item>
          </el-carousel>
        </div>
        <div class="othernewsbox">
          <img src="@/assets/img/othernewsimg.png">
        </div>
        <div class="goveventbox">
          <img src="@/assets/img/govevents.png">
        </div>
      </div>

      <div class="divider"></div>

      <div class="rightbox">
        <div class="commubox">
          <h1 class="link1">交流互动</h1>
          <h1 class="link2">></h1>
          <h1 class="link3">></h1>
          <h1 class="link4">></h1>
        </div>

        <div class="element-divider"></div>

        <div class="mainfunc">
          <div class="xfbanner"><img src="@/assets/img/xfbanner.png"></div>
          <div class="funcs" @click="pageToEvents">
            <img src="@/assets/img/xfts.png">
            <img src="@/assets/img/xfcheck.png">
            <img src="@/assets/img/xfadvance.png">
            <img src="@/assets/img/xfguide.png">
          </div>
        </div>
        <div class="announcebox">
          <img src="@/assets/img/announce.png">
        </div>
        <div class="recomandbox">
          <img src="@/assets/img/recommend.png">
        </div>
      </div>


    <div id="chat-box" v-if="chatOpen">
      <div class="chat-window">
        <div class="chat-header">
          <span>在线客服</span>
          <button @click="closeChat">关闭</button>
        </div>
        <div class="chat-content">
          <ul>
            <li v-for="(msg, index) in messages" :key="index">
              <div :class="msg.type === 'user' ? 'userMessage' : 'aiMessage'">
                <p>{{ msg.message }}</p>
              </div>
            </li>
          </ul>
        </div>
        <div class="chat-input">
          <input type="text" v-model="question" placeholder="请输入您的问题">
          <button @click="sendMessage">发送</button>
        </div>
      </div>
    </div>
      <div id="floating-ball" @click="openChat" v-if="!chatOpen">
        <img src="@/assets/img/chaticon.png" alt="聊天" class="ball-icon">
      </div>
  </div>


    <div class="bottombar">
      <p class="contact-info">联系我们: 123-456-7890 | 123@example.com</p>
      <ul class="social-links">
        <li><a href="#">官方小程序</a></li>
        <li><a href="#">微信公众号</a></li>
        <li><a href="#">地址</a></li>
        <li><a href="#">邮编</a></li>
      </ul>
      <p>&copy; 2023 智慧信访平台. 版权所有.</p>
    </div>
  </div>
</template>

<script setup>
import router from "@/router";
import { onMounted,ref } from "vue";
import { useTokenStore } from "@/stores/token";
import { ElMessage } from "element-plus";
import { getInfoService } from "@/api/user";
import { usePhoneStore } from "@/stores/phone";

const phoneStore = usePhoneStore();
const isLogin = ref(true);
const chatOpen = ref(false);
const userId = ref('');
const question = ref();
const messages = ref([
  {type:'user',message:'您好，我想问你一些问题。'},
  {type:'ai',message:'你好，请问有什么问题需要咨询？'}
])
let socket;
let answer;

onMounted(() => {
    createSocket();
});
// async function createSocket(){
//   await getUserId();
//     socket = new WebSocket(`wss://192.168.58.147:8443/websocket/chat/${userId.value}`)
//     socket.onopen = function(){
//         console.log('websocket打开');
//     }
//     socket.addEventListener = ('message',(event)=>{
//       console.log(event);
//       answer += event.data;
//       messages.value.push({'type':'ai','message':answer});
//     });
// }
async function createSocket() {
  await getUserId();
  if (!userId.value) {
    console.error('User ID is not defined');
    return;
  }

  const socket = new WebSocket(`wss://192.168.58.147:8443/websocket/chat/${userId.value}`);

  socket.onopen = function() {
    console.log('WebSocket connection opened');
  };

  socket.onmessage = function(event) {
    console.log('Message from server:', event.data);
    answer += event.data;
    messages.value.push({ type: 'ai', message: answer });
  };

  socket.onerror = function(error) {
    console.error('WebSocket error:', error);
  };

  socket.onclose = function(event) {
    console.log('WebSocket connection closed', event);
  };
}

//判断是否登陆
const tokenStore = useTokenStore();
if (tokenStore.token) {
  isLogin.value = false;
}

//退出登录
const logout = () => {
  tokenStore.removeToken();
  isLogin.value = true;
  router.push('./');
  ElMessage.success("您已退出登录");
};

//页面跳转相关函数

//登陆注册
const pageToLogin = () => {
  router.push('./login');
};
//
const pageToRegister = () => {
  router.push('./login');
};

const pageToInfo = () => {
  router.push('./info');
};

const pageToCNews = () => {
  router.push('https://www.gov.cn/');
};

const pageToVideoTalk = () => {
  router.push('./videoTalk');
};

const pageToEvents = () => {
  if (tokenStore.token)
    router.push('./events');
  else {
    ElMessage.warning("请先登录");
    router.push('/login');
  }
};

const openChat = () => {
  chatOpen.value = true;
};

const closeChat = () => {
  chatOpen.value = false;
};

const getUserId = async () =>{
  const userInfo = await getInfoService(phoneStore.phone);
  userId.value = userInfo.data.data.userId;
}

const sendMessage = async() => {
  answer = '';
  messages.value.push({'type':'user','message':question.value}); 
  socket.send(JSON.stringify({
            question: question.value
        }));
  question.value = ''
};

</script>


<style>
.base{
 background-color:#f5f2f2;
}
.topbar{
  background-color: #0163d1;
  height: 12%;
  width: 100%;
  position: absolute;
  top: 0%;
  display: flex;
}
.logobox{
  height: 100%;
  margin-left: 3%;
  width:25%;
}
.logobox h1{
  font-size:45px;
  font-weight: 900;
  color: white;
  margin-top:2%;
  margin-left: 6%;
  letter-spacing: 12px;
}
.navbarbox{
  
  width:40%;
  height: 100%;
  margin-left:5%;
  padding-top: 2%;
  font-weight: 500;
}

/* 导航条样式 */
  .nav {
    display: flex;
    justify-content: center;
    list-style-type: none; /* 移除列表项的默认样式 */
    padding: 0;
    
  }

  /* 导航项样式 */
  .nav li {
    padding: 10px 20px; /* 导航项内边距 */
  }

  /* 链接样式 */
  .nav a {
    text-decoration: none; /* 移除下划线 */
    color: #f5f2f2; /* 字体颜色 */
    font-size: 20px; /* 增大默认字体大小 */
    font-weight: bold; /* 字体加粗 */
    transition: font-size 0.3s, font-weight 0.3s;
  }

  /* 鼠标悬停效果 */
  .nav a:hover {
    font-size: 25px; /* 鼠标悬停时字体变大 */
    font-weight: bolder;
  }


.loginbox{
  width:25%;
  height: 100%;
  margin-left:2%;
}
/* 导航项样式 */
.login-nav li {
    padding: 5px 5px; /* 导航项内边距 */
  }
.login-nav{
    display: flex;
    justify-content: center;
    list-style-type: none; /* 移除列表项的默认样式 */
    padding: 0;
    margin-top:10%;
    transition: font-size 0.5s, font-weight 0.5s;
}
.login-nav a {
    text-decoration: none; /* 移除下划线 */
    color: #f5f2f2; /* 字体颜色 */
    font-size: 12px; /* 增大默认字体大小 */
    font-weight: normal; /* 字体加粗 */
  }
/* 鼠标悬停效果 */
.login-nav a:hover {
    font-size: 20px; /* 鼠标悬停时字体变大 */
    font-weight: bold;
  }


.bodybox{
  background-color: rgb(252, 252, 255);
  width: 94%;
  margin-left: 3%;
  height: 1160px;
  display: flex;
}

.img{
  width: 100%;
}

.topbanner{
  background-image: linear-gradient(to top, #990000, #ff6666);
  width:90%;
  left: 5%;
  height:15%;
  position: absolute;
  top:15%;
}
.topbanner h1{
  font-size:55px;
  font-weight: 700;
  color: white;
  margin-top:1%;
  margin-left: 8%;
  letter-spacing: 12px;
  font-family: 'FangSong', '仿宋', 'SimSun', sans-serif;
}
.topbanner img{
  float: left;
  width:80px;
  margin-top:1%;
  margin-left:2%;
}
.leftbox{
  flex:6;
  height: 900px;
  width:80%;
  margin-top:18%;
}

/*分割线 */
.divider {
  width: 2px; /* 分线宽度 */
  background-color: #2154cc; /* 分线颜色 */
  margin: 0 10px; /* 左右边距，根据需要调整 */
  height: 900px; /* 确保分割线高度占满元素的高度 */
  margin-top:18%;
  display: inline-block; /* 使元素内联显示 */
  vertical-align: middle; /* 垂直居中对齐 */
}
/*分割线 */
.element-divider {
  border-bottom: 1px solid #9dcde7; /* 灰色分割线 */
  height: 1px; /* 分线高度 */
  margin: 10px 5%; /* 上下边距 */
}

.element {
  display: inline-block; /* 使元素内联显示 */
  vertical-align: middle; /* 垂直居中对齐 */
}

.rightbox{
  margin-top:18%;
  flex:4;
  height: 900px;
  width:80%;
}
.searchbox{
  margin-left: 5%;
  width: 90%;
  height: 5%;
}

  /* 搜索框容器样式 */
  .search-box {
    display: flex; /* 使用flex布局 */
    align-items: center; /* 垂直居中对齐 */
  }

  /* 输入框样式 */
  .search-input {
    flex-grow: 1; /* 填充可用空间 */
    padding: 8px; /* 内边距 */
    border: none; /* 无边框，外边框由容器提供 */
    outline: none; /* 点击时无轮廓 */
    background-color: #ccc;
  }

  /* 搜索按钮样式 */
  .search-button {
    padding: 8px 16px; /* 内边距 */
    background-color: #007bff; /* 背景颜色 */
    color: white; /* 字体颜色 */
    border: none; /* 无边框 */
    cursor: pointer; /* 鼠标悬停时显示手形图标 */
    transition: background-color 0.3s; /* 背景颜色变化的过渡效果 */
  }

  .search-button:hover {
    background-color: #0056b3; /* 鼠标悬停时背景颜色变深 */
  }

.mainnewsbox{
  margin-left: 5%;
  width: 90%;
  background-color: aliceblue;
  height: 48%;
  margin-top:2%
}

/*主要新闻轮播*/

.el-carousel__item h3 {
  color: #475669;
  font-size: 18px;
  opacity: 0.75;
  line-height: 100%;
  margin: 0;
  display: none; /* 隐藏数字标题 */
}

.el-carousel__item {
  background: none; /* 移除背景色 */
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  width: 100%;
  
}

.el-carousel__item img {
  
  height: 100%;
  
}

.othernewsbox{
  margin-left: 5%;
  width: 90%;
  background-color: aliceblue;
  height: 22%;
  margin-top:1%;
}
.goveventbox{
  margin-left: 5%;
  width: 90%;
  background-color: aliceblue;
  margin-top:1%;
  height: 18%;
}

/*右边块 */
.commubox{
  margin-left: 5%;
  width: 90%;
  background-color: aliceblue;
  height: 5%;
  display: flex;
  padding-left:3%;
  padding-top:2%;
}

.link1{
  color:#0163d1;
  font-size: 20px;
  font-weight: 700;
}
.link2{
  color: #0d39d7;
  font-size: 16px;
  margin-top:1%;
}
.link3{
  color: #3482db;
  font-size: 16px;
  margin-top:1%;
}
.link4{
  color: #6ea2e1;
  font-size: 16px;
  margin-top:1%;
}

.mainfunc{
  margin-left: 5%;
  width: 90%;
  background-color: aliceblue;
  height: 48%;
  margin-top:2%;
  display:flex;
  flex-direction: column;
}

.xfbanner{
  width: 100%;
}
.xfbanner img{
  width: 100%;
  
}
.funcs{
  margin-top: auto;
  display: flex; /* 使用Flexbox布局 */
  flex-wrap: wrap; /* 允许元素换行 */
  justify-content: center; /* 水平居中*/
}
.funcs img{
  width: calc(50% - 10px); /* 图片宽度设为容器宽度的一半减去间隔 */
  margin-bottom: 10px; /* 图片底部边距 */
  margin-right: 10px; /* 图片之间的水平间隔，除了最后一列 */
  box-sizing: border-box; /* 边框计算在宽度内 */
}


.announcebox{
  margin-left: 5%;
  width: 90%;
  background-color: aliceblue;
  height: 25%;
  margin-top:2%;
}
.recomandbox{
  margin-left: 5%;
  width: 90%;
  background-color: aliceblue;
  height: 18%;
}
/*底栏 */
.bottombar{
  height: 100px;
  width:94%;
  margin-left:3%;
  background-color: #0864d4;
  margin-top: 0%;
  text-align: center;
  color:#f5f2f2;
  padding-top:20px;
}

.bottombar p {
            margin: 0; /* 移除段落的外边距 */
        }

        /* 联系信息样式 */
        .contact-info {
            margin-bottom: 10px; /* 底部边距 */
        }

        /* 社交媒体链接样式 */
        .social-links {
            list-style: none; /* 移除列表的默认样式 */
            padding: 0;
        }

        .social-links li {
            display: inline; /* 内联显示列表项 */
            margin: 0 10px; /* 列表项之间的间距 */
        }

        .social-links a {
            color: inherit; /* 链接颜色继承自父元素 */
            text-decoration: none; /* 移除下划线 */
            transition: color 0.3s; /* 颜色变化的过渡效果 */
        }

        .social-links a:hover {
            color: #0864d4; /* 鼠标悬停时链接颜色 */
        }
/* 悬浮球的样式 */
#floating-ball {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 100; /* 确保悬浮球在其他元素之上 */
  cursor: pointer;
}

#floating-ball .ball-icon {
  width: 50px; /* 悬浮球大小 */
  height: 50px;
  border-radius: 50%; /* 设置圆形 */
  background-color: #0864d4; /* 设置背景颜色 */
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2); /* 设置阴影 */
  display: flex;
  justify-content: center;
  align-items: center;
}

#chat-box {
  position: fixed;
  bottom: 20px;
  right: 20px;
  width: 300px;
  max-height: 400px;
  background-color: white;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
  z-index: 100;
  border-radius: 5px;
  overflow-y: auto;
}

.chat-window {
  padding: 10px;
}

.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chat-content {
  height: 250px;
  overflow-y: auto;
}

.chat-content ul {
  list-style: none;
  padding: 0;
}

.chat-content li {
  margin-bottom: 10px;
}

.message {
  padding: 10px;
  border-radius: 5px;
}

.aiMessage {
  background-color: #f0f0f0;
  text-align: left;
}

.userMessage {
  background-color: #e0f2ff;
  text-align: right;
}

.chat-input {
  display: flex;
  align-items: center;
  margin-top: 10px;
}

.chat-input input {
  flex-grow: 1;
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 5px;
}

.chat-input button {
  padding: 8px 16px;
  background-color: #0864d4;
  color: white;
  border: none;
  cursor: pointer;
  border-radius: 5px;
}

</style>
